package com.bjpowernode.config.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.bjpowernode.config.context.ApplicationContextProvider;
import com.bjpowernode.constant.Constants;
import com.bjpowernode.entity.TDicType;
import com.bjpowernode.entity.TDicValueDO;
import com.bjpowernode.service.DicTypeService;
import org.springframework.stereotype.Component;

import javax.print.DocFlavor;
import java.util.List;

/**
 * 称呼的转换器
 *
 * Excel中的“先生”字符串 转为 Java中的Integer
 *
 */
public class AppellationConverter implements Converter<Integer> {

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        int result = -1; //-1表示数据库不存在该称呼值
        //拿到称呼的中文名称：“先生”、“女士”
        String appellationName = cellData.getStringValue();
        //把称呼的中文名称 转成 对应的id值

        //拿到spring容器中的DicTypeService对象，然后调用里面的查询“称呼”字典值的方法，拿到称呼的字典数据
        DicTypeService dicTypeService = ApplicationContextProvider.getBean(DicTypeService.class);
        TDicType tDicType = dicTypeService.getDicTypeByTypeCode(Constants.APPELLATION);
        //拿到称呼的List,有5个称呼值
        List<TDicValueDO> dicValueDOList = tDicType.getDicValueDOList();

        for (TDicValueDO tDicValueDO : dicValueDOList) {
            Integer id = tDicValueDO.getId();
            String typeValue = tDicValueDO.getTypeValue();
            if (appellationName.equals(typeValue)) {
                result = id;
                break;
            }
        }
        return result;
    }
}
